package com.leantech.android.browser.ui.search.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.cscj.android.repository.db.entity.HotSearchEntity
import com.google.android.flexbox.FlexboxLayoutManager
import com.leantech.android.browser.databinding.ItemSearchRecommendSectionBinding
import com.leantech.android.browser.utils.onThrottleClick
import com.leantech.android.browser.utils.show
import okhttp3.internal.toImmutableList

class SearchRecommendSectionAdapter(private val adapterCallback: SearchRecommendAdapter.SearchRecommendItemAdapterCallback) :
    RecyclerView.Adapter<SearchRecommendSectionAdapter.SearchRecommendSectionViewHolder>() {

    private val data = mutableListOf<HotSearchEntity>()

    private var visible = true

    companion object {
        private const val PayloadVisible = 1
    }

    @SuppressLint("NotifyDataSetChanged")
    fun submitList(list: List<HotSearchEntity>) {
        data.clear()
        data.addAll(list)
        notifyDataSetChanged()
    }

    fun toggleVisible() {
        visible = !visible
        notifyItemChanged(0, PayloadVisible)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup, viewType: Int
    ): SearchRecommendSectionViewHolder {
        val binding = ItemSearchRecommendSectionBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        )
        return SearchRecommendSectionViewHolder(binding)
    }

    override fun onBindViewHolder(holder: SearchRecommendSectionViewHolder, position: Int) {
        holder.bind(data.toImmutableList(), visible, adapterCallback)
    }

    override fun onBindViewHolder(
        holder: SearchRecommendSectionViewHolder, position: Int, payloads: MutableList<Any>
    ) {
        if (payloads.isEmpty()) {
            super.onBindViewHolder(holder, position, payloads)
        } else {
            payloads.forEach { payload ->
                when (payload) {
                    PayloadVisible -> {
                        holder.updateVisible(visible)
                    }

                    else -> {
                        super.onBindViewHolder(holder, position, payloads)
                    }
                }
            }
        }

    }

    override fun getItemCount(): Int {
        return if (data.isEmpty()) 0 else 1
    }


    class SearchRecommendSectionViewHolder(private val binding: ItemSearchRecommendSectionBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(
            list: List<HotSearchEntity>,
            visible: Boolean,
            adapterCallback: SearchRecommendAdapter.SearchRecommendItemAdapterCallback
        ) {
            if (list.isEmpty()) return
            val adapter = binding.recyclerView.adapter
            if (adapter == null) {
                binding.recyclerView.adapter = SearchRecommendAdapter(adapterCallback)
                binding.recyclerView.layoutManager = FlexboxLayoutManager(itemView.context)
            }

            submitList(list)
            updateVisible(visible)

            binding.btnToggle.onThrottleClick {
                val sectionAdapter =
                    bindingAdapter as? SearchRecommendSectionAdapter ?: return@onThrottleClick
                sectionAdapter.toggleVisible()
            }

        }

        private fun submitList(list: List<HotSearchEntity>) {
            val adapter = binding.recyclerView.adapter as? SearchRecommendAdapter ?: return
            adapter.submitList(list)
        }

        fun updateVisible(visible: Boolean) {
            binding.recyclerView.show(visible)
            binding.btnToggle.isSelected = !visible
        }

    }

}